package com.sc.core.function;

import lombok.SneakyThrows;

import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;

@FunctionalInterface
public interface Function <T, R> extends java.util.function.Function<T, R>, Serializable {

    @SneakyThrows
    default SerializedLambda getSerializedLambda(){
        Method method = this.getClass().getDeclaredMethod("writeReplace");
        method.setAccessible(Boolean.TRUE);
        return (SerializedLambda) method.invoke(this);
    }

    default String getMethodName(){
        return getSerializedLambda().getImplMethodName();
    }

    @SneakyThrows
    default String getFieldName(){
        boolean isRecord = Class.forName(getClassName()).isRecord();
        if (isRecord) {
            return getMethodName();
        }
        return getMethodName().substring(3, 4).toLowerCase() + getSerializedLambda().getImplMethodName().substring(4);
    }

    default String getClassName(){
        return getSerializedLambda().getImplClass().replaceAll("/", ".");
    }
}

